<template>
  <el-container>
    <el-main>
      <h1 align="center">采购单管理</h1>
      <el-form :inline="true" :model="form" size="mini" align="center">
        <el-form-item label="客户名称:">
          <el-input v-model="form.customerName" />
        </el-form-item>
        <el-form-item label="采购未进数量是否过期:">
          <el-select v-model="form.quantityOverdue" :clearable="true">
            <el-option label="已过期" value="已过期" />
            <el-option label="未过期" value="未过期" />
          </el-select>
        </el-form-item>
        <el-form-item label="开单时间:">
          <el-date-picker
            v-model="form.time"
            align="right"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-button type="primary" size="mini" @click="loadData()">查询</el-button>
        <el-button type="warning" size="mini" @click="selectPrinting(1)">选择打印</el-button>
        <el-button type="warning" size="mini" @click="wholePrinting(2)">整页打印</el-button>
        <el-button type="success" size="mini" @click="toExcel">Excel导出</el-button>
        <el-button type="primary" size="small" @click="addMaterial()">生成辅料单</el-button>
      </el-form>
      <div>
        <el-table
          ref="singleTable"
          :data="tableData"
          highlight-current-row
          style="width: 100%"
          border=""
          align="center"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column v-show="true" prop="no" label="任务编号" width="140" />
          <el-table-column v-show="true" prop="name" label="客户名称" width="140" />
          <el-table-column v-show="true" prop="customerNo" label="客户单号" width="140" />
          <el-table-column v-show="true" prop="stare" label="楞型" width="140" />
          <el-table-column label="材质" prop="material" width="250">
            <template slot-scope="scope">
              {{ scope.row.material }}
            </template>
          </el-table-column>
          <el-table-column v-show="true" prop="length" label="长" width="140" />
          <el-table-column v-show="true" prop="width" label="宽" width="140" />
          <el-table-column v-show="true" prop="height" label="高" width="140" />
          <el-table-column v-show="true" prop="pressureSpecification" label="分压规格" width="140" />
          <el-table-column v-show="true" prop="orderNum" label="订单数量" width="140" />
          <el-table-column v-show="true" prop="unit" label="单位" width="140" />
          <el-table-column v-show="true" prop="isPurchase" label="采购单是否生成" width="140" />
          <el-table-column v-show="true" prop="documentsNo" label="采购单号" width="140" />
          <el-table-column v-show="true" prop="purchaseQuantity" label="采购数量" width="140" />
          <el-table-column v-show="true" prop="costPrice" label="成本价" width="140" />
          <el-table-column v-show="true" prop="amount" label="金额" width="140" />
          <el-table-column v-show="true" prop="position" label="仓位" width="140" />
          <el-table-column v-show="true" prop="endProductPos" label="成品仓位" width="140" />
          <el-table-column v-show="true" prop="returnNum" label="退货数量" width="140" />
          <el-table-column label="操作" width="720">
            <template slot-scope="scope">
              <el-button type="danger" style="width:90px" size="mini" :disabled="scope.row.documentsNo!==null ?false : true" @click="drop(scope)">删除采购单</el-button>
              <el-button type="primary" size="mini" style="width:90px" :disabled="scope.row.documentsNo!==null ?true : false" @click="purAdd(scope)">生成采购单</el-button>
              <el-button type="primary" size="mini" style="width:90px" :disabled="(scope.row.position==='0' ?false : true)||(scope.row.endProductPos==='0' ?false : true)" @click="modifyPur(scope)">编辑采购单</el-button>
              <el-button type="primary" size="mini" style="width:60px" :disabled="(scope.row.position==='0' ?false : true)||(scope.row.endProductPos==='0' ?false : true)" @click="warehousing(scope)">入库</el-button>
              <el-button type="primary" size="mini" style="width:90px" @click="warehousing_printing(scope)">入库单打印</el-button>
              <el-button type="primary" size="mini" style="width:90px" :disabled="scope.row.documentsNo!==null ?false : true" @click="addReturn(scope)">生成退货单</el-button>
              <el-button type="warning" size="mini" style="width:90px" :disabled="scope.row.documentsNo!==null ?false : true" @click="printing(scope)">生成打印单</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <el-pagination
          background
          layout="total, sizes, prev, pager, next"
          :total="pagination.total"
          :current-page="pagination.page"
          :page-size="pagination.size"
          align="center"
          @size-change="sizeChange"
          @current-change="pageChange"
        />
      </div>
      <!-- 入库单打印对话框 -->
      <el-dialog
        title="纸板入仓打印"
        :visible.sync="storage_dialogVisible"
        width="30%"
      >
        <div id="printTest">
          <table>
            <tr>
              <td colspan="4">
                <h3 class="ups">{{ name2 }}</h3>
                <h3 class="ups">纸板入仓标签</h3>
              </td>
            </tr>
            <tr>
              <td style="color:#080e11">任务编号</td>
              <td colspan="3">
                <span class="up">{{ formData.no }}</span>
              </td>
            </tr>
            <tr>
              <td>客户名称</td>
              <td colspan="3">
                <span class="up">{{ formData.name }}</span>
              </td>
            </tr>
            <tr>
              <td>客户单号</td>
              <td colspan="3">
                <span class="up">{{ formData.customerNo }}</span>
              </td>
            </tr>
            <tr>
              <td>长*宽*高</td>
              <td>
                <span class="up">{{ formData.length }}*{{ formData.width }}*{{ formData.height }}</span>
              </td>
              <td>愣型</td>
              <td>
                <span class="up">{{ formData.stare }}</span>
              </td>
            </tr>
            <tr>
              <td>材质</td>
              <td colspan="3">
                <span class="up">{{ formData.material }}</span>
              </td>
            </tr>
            <tr>
              <td>订单数量</td>
              <td>
                <span class="up">{{ formData.orderNum }}</span>
              </td>
              <td>入仓量</td>
              <td>
                <span class="up">{{ formData.purchaseQuantity }}</span>
              </td>
            </tr>
            <tr>
              <td>交货日期</td>
              <td colspan="3">
                <span class="up">{{ formData.deliveryDate }}</span>
              </td>
            </tr>
            <!-- <tr>
              <td>入库日期</td>
              <td colspan="3">
                <span class="up">{{ dataTime }}</span>
              </td>
            </tr> -->
            <tr>
              <td>堆码</td>
              <td colspan="3">
                <span class="up">第&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;堆/共&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;堆</span>
              </td>
            </tr>
            <tr>
              <td>备注</td>
              <td colspan="3">
                <span class="up">{{ formData.remark }}</span>
              </td>
            </tr>
          </table>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="storage_dialogVisible = false">取 消</el-button>
          <el-button v-print="'#printTest'" type="primary">确认打印</el-button>
        </span>
      </el-dialog>
      <!-- 新增/编辑采购单 -->
      <el-dialog :title="titleType+'采购单'" :visible.sync="purAddVisible">
        <el-form ref="purForm" :rules="purRules" :inline="true" :model="formAdd" size="mini" label-width="130px">
          <el-form-item v-if="!flag" label="供方">
            <el-select v-model="formAdd.supplierId">
              <el-option
                v-for="item in supplierFor"
                :key="item.id"
                :label="item.fullName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="flag && !flag2" label="里纸供方">
            <el-select v-model="formAdd.insuppier">
              <el-option
                v-for="item in supplierFor"
                :key="item.id"
                :label="item.fullName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-if="flag && flag2" label="表纸供方">
            <el-select v-model="formAdd.outsuppier">
              <el-option
                v-for="item in supplierFor"
                :key="item.id"
                :label="item.fullName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="客户名称" prop="customerName">
            <el-input v-model="formAdd.customerName" disabled />
          </el-form-item>

          <el-form-item label="任务编号">
            <el-input v-model="formAdd.taskNumber" disabled />
          </el-form-item>

          <el-form-item label="客户单号">
            <el-input v-model="formAdd.customerNo" disabled />
          </el-form-item>

          <el-form-item label="计价方式">
            <el-select v-model="formAdd.pricingMethod">
              <el-option label="净边" value="净边" />
              <el-option label="净宽" value="净宽" />
            </el-select>
          </el-form-item>
          <el-form-item label="开单日期" prop="billingDate">
            <el-date-picker
              v-model="formAdd.billingDate"
              align="right"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期"
            />
          </el-form-item>

          <el-form-item label="结算周期" prop="settlementPeriod">
            <el-input-number v-model="formAdd.settlementPeriod" :controls="false" placeholder="单位（天）" />
          </el-form-item>

          <el-form-item label="是否成品">
            <el-checkbox v-model="formAdd.isProduct" disabled />
          </el-form-item>

          <el-form-item v-if="!isProducts" label="原材料仓位">
            <el-input-number v-model="formAdd.position" :controls="false" disabled />
          </el-form-item>

          <el-form-item v-if="isProducts" label="成品仓位">
            <el-input-number v-model="formAdd.endProductPos" :controls="false" disabled />
          </el-form-item>

          <el-form-item label="订单数量" prop="orderQuantity">
            <el-input v-model="formAdd.orderQuantity" disabled />
          </el-form-item>

          <el-form-item label="采购原材料数量" prop="purchaseQuantity">
            <el-input v-model="formAdd.purchaseQuantity" @change="purchaseSelect" />
          </el-form-item>

          <el-form-item label="成本价" prop="costPrice">
            <el-input v-model="formAdd.costPrice" @change="purchaseSelect" />
          </el-form-item>

          <el-form-item label="金额">
            <el-input-number v-model="formAdd.amount" :controls="false" disabled />
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="formAdd.remark" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="small" @click="purAddNo('purForm')">取 消</el-button>
          <el-button size="small" type="primary" @click="purAddOk('purForm')">确 定</el-button>
        </span>
      </el-dialog>
    </el-main>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form ref="supForm" :inline="true" :model="formMaterial" size="mini">
        <el-form-item label="日期:" prop="date">
          <el-date-picker
            v-model="formMaterial.date"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="供方:" prop="supplier">
          <el-select v-model="formMaterial.supplier">
            <el-option
              v-for="item in supplierFor"
              :key="item.id"
              :label="item.fullName"
              :value="item.fullName"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="采购单号:" prop="no">
          <el-select v-model="formMaterial.no" @change="purNoChange">
            <el-option
              v-for="item in noFor"
              :key="item.id"
              :label="item.documentsNo"
              :value="item.documentsNo"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="备注:" prop="note">
          <el-input v-model="formMaterial.note" />
        </el-form-item>
        <el-form-item label="品名规格:" prop="specification">
          <el-select v-model="formMaterial.specificationId" placeholder="请选择" @change="specificationChange">
            <el-option
              v-for="item in specificationFor"
              :key="item.value"
              :label="item.specification"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="单位:" prop="unit">
          <el-input v-model="formMaterial.unit" disabled />
        </el-form-item>
        <el-form-item
          label="数量:"
          prop="num"
        >
          <el-input-number v-model="formMaterial.num" :controls="false" @change="numChange" />
        </el-form-item>
        <el-form-item label="单价:" prop="perPrice">
          <el-input-number v-model="formMaterial.perPrice" :controls="false" @change="numChange" />
        </el-form-item>
        <el-form-item label="金额:" prop="money">
          <el-input-number v-model="formMaterial.money" :controls="false" disabled />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="supplierAddNo('supForm')">取 消</el-button>
        <el-button type="primary" @click="supplierAddOk('supForm')">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="returnDialogVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form ref="supForm" :rules="purRules" :inline="true" :model="formReturn" size="mini">
        <el-form-item label="供方:" prop="supplier">
          <el-input v-model="formReturn.supplier" disabled />
        </el-form-item>
        <el-form-item label="采购单号:" prop="no">
          <el-input v-model="formReturn.no" disabled />
        </el-form-item>
        <el-form-item label="备注:" prop="note">
          <el-input v-model="formReturn.note" />
        </el-form-item>
        <el-form-item label="采购数量:" prop="purchaseQuantity">
          <el-input v-model="formReturn.purchaseQuantity" disabled />
        </el-form-item>
        <el-form-item v-if="formReturn.isProduct==='成品' ?false : true" label="仓位:" prop="position">
          <el-input v-model="formReturn.position" disabled />
        </el-form-item>
        <el-form-item v-if="formReturn.isProduct==='成品' ?true : false" label="成品仓位">
          <el-input-number v-model="formReturn.endProductPos" :controls="false" disabled />
        </el-form-item>
        <el-form-item label="退货数量:" prop="returnNum">
          <el-input v-model="formReturn.returnNum" @change="returnNumChange" />
        </el-form-item>
        <el-form-item label="成本价:" prop="costPrice">
          <el-input v-model="formReturn.costPrice" disabled />
        </el-form-item>
        <el-form-item label="退货金额:" prop="returnMoney">
          <el-input v-model="formReturn.returnMoney" disabled />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="returnNo('supForm')">取 消</el-button>
        <el-button type="primary" @click="returnOk('supForm')">确 定</el-button>
      </span>
    </el-dialog>

  </el-container>

</template>

<script>
import initData from '@/mixins/initData'
import { export2Excel } from '@/utils/common'
import { supplierSelect } from '@/api/supplier-cardboard-quotation/cardboard'
import { purNoSelect } from '@/api/supplier-cardboard-quotation/cardboard'
import { add } from '@/api/purchase/purchase'
import { returnAdd } from '@/api/purchase/purchase'
import { list } from '@/api/purchase/purchase'
import { getById } from '@/api/purchase/purchase'
import { specificationList } from '@/api/accessories/means'
import { removeById } from '@/api/purchase/purchase'
import { warehousing } from '@/api/purchase/purchase'
import { addMaterial } from '@/api/accessories/warehousing'
import { name } from '@/api/basedata/firm'

export default {
  name: 'PurchaseOrder',
  mixins: [initData],
  data() {
    return {
      flag: false,
      flag2: false,
      formAdd: {},
      formData: {},
      tableData: [],
      addTableData: [],
      customerFor: [],
      purAddVisible: false,
      purRules: {
        supplierId: [{ required: true, message: '该输入为必填项', trigger: 'change' }],
        pricingMethod: [{ required: true, message: '该输入为必填项', trigger: 'change' }],
        billingDate: [{ required: true, message: '该输入为必填项', trigger: 'change' }],
        deliveryDate: [{ required: true, message: '该输入为必填项', trigger: 'change' }],
        customerName: [{ required: true, message: '该输入为必填项', trigger: 'change' }],
        settlementPeriod: [{ required: true, message: '该输入为必填项', trigger: 'change' }],
        purchaseQuantity: [{ required: true, message: '该输入为必填项', trigger: 'blur' }],
        costPrice: [{ required: true, message: '该输入为必填项', trigger: 'change' }],
        returnNum: [{ required: true, message: '该输入为必填项', trigger: 'change' }]

      },
      supplierFor: [],
      pricingFor: [],
      titleType: '',
      taskNumberVisible: false,
      multipleSelection: [],
      form: {
        customerName: '',
        quantityOverdue: '',
        time: ''
      },
      orderDate: '',
      identification: '',
      dialogVisible: false,
      formMaterial: {},
      specificationFor: [],
      noFor: [],
      returnDialogVisible: false,
      formReturn: {},
      isProducts: false,
      storage: true,
      storage_dialogVisible: false,
      dataTime: '',
      war: true,
      name2: '',
      tempArray: []
    }
  },
  created() {
    this.init()
    name().then(res => {
      var firmList = []
      firmList = res.list
      firmList.forEach(a => {
        if (a.isFirm === true) {
          this.name2 = a.name
        }
      })
    })
  },
  methods: {
    returnOk(supForm) {
      this.$refs[supForm].validate((valid) => {
        if (valid) {
          returnAdd(this.formReturn).then(res => {
            this.$message.success(this.titleType + '成功')
            this.init()
          })
          this.returnDialogVisible = false
        } else {
          return false
        }
      })
    },
    returnNo() {
      this.returnDialogVisible = false
    },
    returnNumChange() {
      this.formReturn.returnMoney = this.formReturn.returnNum * this.formReturn.costPrice
    },
    // 退货
    addReturn(scope) {
      this.returnDialogVisible = true
      console.log(scope.row)
      this.formReturn.no = scope.row.documentsNo
      this.formReturn.costPrice = scope.row.costPrice
      this.formReturn.id = scope.row.pid
      this.formReturn.supplier = scope.row.supplier
      this.formReturn.position = scope.row.position
      this.formReturn.endProductPos = scope.row.endProductPos
      this.formReturn.purchaseQuantity = scope.row.purchaseQuantity
      this.formReturn.isProduct = scope.row.isProduct
      this.formReturn.position = scope.row.position
    },
    numChange() {
      this.$set(this.formMaterial, 'money', parseInt(this.formMaterial.num) * parseInt(this.formMaterial.perPrice))
    },
    specificationChange() {
      this.specificationFor.forEach(a => {
        if (a.id === this.formMaterial.specificationId) {
          this.formMaterial.unit = a.unit
        }
      })
    },
    // 选择采购单号回调数量和单价
    purNoChange() {
      this.noFor.forEach(a => {
        if (a.documentsNo === this.formMaterial.no) {
          this.formMaterial.num = a.purchaseQuantity
          this.formMaterial.perPrice = a.costPrice
          this.formMaterial.money = this.formMaterial.num * this.formMaterial.perPrice
        }
      })
    },
    // 新增辅料
    addMaterial() {
      this.dialogVisible = true
      this.formMaterial = {}
      specificationList().then(res => {
        this.specificationFor = res
      })
      // 加载供应商下拉框
      supplierSelect().then(res => {
        this.supplierFor = res
      })
      // 加载采购单号下拉框
      purNoSelect().then(res => {
        this.noFor = res
      })
    },
    // 新增保存
    supplierAddOk(supForm) {
      this.$refs[supForm].validate((valid) => {
        if (valid) {
          addMaterial(this.formMaterial).then(res => {
            this.$message.success(this.titleType + '成功')
            this.$refs[supForm].resetFields()
            this.loadData()
          })
          this.dialogVisible = false
        } else {
          return false
        }
      })
    },
    // 新增取消
    supplierAddNo(supForm) {
      this.dialogVisible = false
      this.formMaterial = {}
      this.$refs[supForm].resetFields()
    },
    // 采购数量改变金额改变
    purchaseSelect() {
      this.formAdd.amount = this.formAdd.costPrice * this.formAdd.purchaseQuantity
    },
    loadData() {
      this.queryParams.customerName = this.form.customerName
      this.queryParams.quantityOverdue = this.form.quantityOverdue
      this.queryParams.time = this.form.time
      if (this.queryParams.time === null) {
        this.$set(this.queryParams, 'time', '')
      }
      list(this.queryParams).then(res => {
        this.tableData = res.list
        console.log(res.list)
        this.pagination.total = res.total
        this.tableData.forEach(a => {
          var str = ''
          if (a.productType === '水印' || a.productType === '挡板') {
            const jsonArray = JSON.parse(a.material)
            jsonArray.forEach(data => {
              str += data + ' ,'
            })
            str = str.substring(0, str.length - 1)
          } else if (a.productType === '彩印') {
            str += (a.colorInMaterial === '' ? '' : '里纸: ' + a.colorInMaterial) + (a.colorOutMaterial === '' ? '' : ' 表纸: ' + a.colorOutMaterial)
          }
          a.material = str
          if (a.documentsNo !== null) {
            a.isPurchase = '已生成采购单'
          } else {
            a.isPurchase = '未生成采购单'
          }
        })
      })
    },
    // 导出
    toExcel() {
      var list = this.tableData
      const th = ['任务编号', '客户名称', '客户单号', '楞型', '材质', '长', '宽', '高', '分压规格', '订单数量', '单位', '采购单是否生成', '采购单号', '采购数量', '成本价', '金额', '仓位', '成品仓位', '退货数量']
      const filterVal = ['no', 'name', 'customerNo', 'stare', 'material', 'length', 'width', 'height', 'pressureSpecification', 'orderNum', 'unit', 'isPurchase', 'documentsNo', 'purchaseQuantity', 'costPrice', 'amount', 'position', 'endProductPos', 'returnNum']
      const data = list.map(v => filterVal.map(k => v[k]))
      export2Excel(th, data, '采购单导出')
    },
    // 选择打印
    selectPrinting(val) {
      // 判断是否为相同客户 1
      for (var i = 0; i < this.tempArray.map(x => x.name).length; i++) {
        for (var j = i + 1; j < this.tempArray.map(x => x.name).length; j++) {
          if (this.tempArray.map(x => x.name)[i] !== this.tempArray.map(x => x.name)[j]) {
            return this.$message.error('只有选择相同客户才能打印!!!')
          } else {
            this.multipleSelection[0].b = val
            // console.log('相同客户', val)
          }
        }
      }
      if (this.form.quantityOverdue === '已过期') {
        let flag = true
        this.multipleSelection.forEach(a => {
          if (a.documentsNo === null) {
            this.$message.error('没有采购内容！！！')
            flag = false
          }
        })
        if (flag) {
          if (this.multipleSelection.length === 0) {
            this.$message.error('请选择打印的内容！！！')
            return flag
          } else {
            this.$router.push({
              path: '/purchase_not_included_overdue',
              query: { 'data': this.multipleSelection }
            })
          }
        }
      } else if (this.form.quantityOverdue === '未过期') {
        let flag = true
        this.multipleSelection.forEach(a => {
          if (a.documentsNo === null) {
            this.$message.error('没有采购内容！！！')
            flag = false
          }
        })
        if (flag) {
          if (this.multipleSelection.length === 0) {
            this.$message.error('请选择打印的内容！！！')
            return flag
          } else {
            this.$router.push({
              path: '/purchase_not_included',
              query: { 'data': this.multipleSelection }
            })
          }
        }
      } else {
        let flag = true
        this.multipleSelection.forEach(a => {
          if (a.documentsNo === null) {
            this.$message.error('没有采购内容！！！')
            flag = false
          }
        })
        if (flag) {
          if (this.multipleSelection.length === 0) {
            this.$message.error('请选择打印的内容！！！')
            return flag
          } else {
            this.$router.push({
              path: '/purchase_order_printing',
              query: { 'data': this.multipleSelection }
            })
          }
        }
      }
    },
    // 整页打印
    wholePrinting(val) {
      this.tableData[0].a = val
      if (this.form.quantityOverdue === '已过期') {
        let flag = true
        this.tableData.forEach(a => {
          if (a.documentsNo === null) {
            this.$message.error('没有采购内容！！！')
            flag = false
          }
        })
        if (flag) {
          if (this.tableData.length === 0) {
            this.$message.error('请选择打印的内容！！！')
            return flag
          } else {
            this.$router.push({
              path: '/purchase_not_included_overdue',
              query: { 'data': this.tableData }
            })
          }
        }
      } else if (this.form.quantityOverdue === '未过期') {
        let flag = true
        this.tableData.forEach(a => {
          if (a.documentsNo === null) {
            this.$message.error('没有采购内容！！！')
            flag = false
          }
        })
        if (flag) {
          if (this.tableData.length === 0) {
            this.$message.error('请选择打印的内容！！！')
            return flag
          } else {
            this.$router.push({
              path: '/purchase_not_included',
              query: { 'data': this.tableData }
            })
          }
        }
      } else {
        let flag = true
        this.tableData.forEach(a => {
          if (a.documentsNo === null) {
            this.$message.error('没有采购内容！！！')
            flag = false
          }
        })
        if (flag) {
          if (this.tableData.length === 0) {
            this.$message.error('请选择打印的内容！！！')
            return flag
          } else {
            this.$router.push({
              path: '/purchase_order_printing',
              query: { 'data': this.tableData }
            })
          }
        }
      }
    },
    // 打印
    printing(scope) {
      if (this.form.quantityOverdue === '已过期') {
        scope.row.orderDate = this.orderDate
        this.multipleSelection.push(scope.row)
        this.$router.push({
          path: '/purchase_not_included_overdue',
          query: { 'data': this.multipleSelection }
        })
      } else if (this.form.quantityOverdue === '未过期') {
        scope.row.orderDate = this.orderDate
        this.multipleSelection.push(scope.row)
        this.$router.push({
          path: '/purchase_not_included',
          query: { 'data': this.multipleSelection }
        })
      } else {
        this.multipleSelection.push(scope.row)
        this.$router.push({
          path: '/purchase_order_printing',
          query: { 'data': this.multipleSelection }
        })
      }
    },
    handleSelectionChange(row) {
      this.multipleSelection = row
      this.tempArray = row
    },
    // 删除
    drop(scope) {
      if (scope.row.position !== '0' || scope.row.endProductPos !== '0') {
        this.$message.error('已入库删除失败')
        return
      } else {
        removeById(scope.row.pid).then(res => {
          if (res) {
            this.$message.success('删除成功')
            this.loadData()
          } else {
            this.$message.error('删除失败')
          }
        })
      }
    },

    // 入库
    warehousing(scope) {
      this.formAdd.id = scope.row.pid
      this.formAdd.isProduct = scope.row.isProduct
      this.formAdd.purchaseQuantity = scope.row.purchaseQuantity
      this.formAdd.deliveryDate = scope.row.deliveryDate
      this.formAdd.boxType = scope.row.boxType
      this.formAdd.stare = scope.row.stare
      this.formAdd.material = scope.row.material
      this.formAdd.orderQuantity = scope.row.orderNum
      this.formAdd.length = scope.row.length
      this.formAdd.width = scope.row.width
      this.formAdd.height = scope.row.height
      this.formAdd.costPrice = scope.row.costPrice
      this.formAdd.taskNumber = scope.row.no
      this.formAdd.endProductPos = scope.row.endProductPos
      this.formAdd.position = scope.row.position
      this.formAdd.customerName = scope.row.id
      this.formAdd.returnNum = scope.row.returnNum
      this.formAdd.supplierId = scope.row.supplierId
      this.formAdd.productNum = scope.row.productNum
      this.formAdd.productNum = scope.row.productNum
      console.log(this.formAdd)
      warehousing(this.formAdd).then(res => {
        if (res) {
          this.identification = this.$message.success('入库成功')
          console.log(res)
          // this.scope.row.disabled = 1
          scope.row.disabled
          this.loadData()
          this.storage = false
        } else {
          this.$message.error('入库失败')
        }
      })
    },
    // 入库单打印
    warehousing_printing(scope) {
      this.formData = scope.row
      this.storage_dialogVisible = true
      var nowDate = new Date()
      var year = nowDate.getFullYear()
      var month = nowDate.getMonth() + 1 < 10 ? '0' + (nowDate.getMonth() + 1)
        : nowDate.getMonth() + 1
      var day = nowDate.getDate() < 10 ? '0' + nowDate.getDate() : nowDate
        .getDate()
      var dateStr = year + '-' + month + '-' + day
      this.dataTime = dateStr
      console.log(this.dataTime)
      // if (scope.row.documentsNo !== null ? true : false && (scope.row.position !== '0') || (scope.row.endProductPos !== '0')) {
      //   this.war = false
      // }
    },
    // 编辑订单
    modifyPur(scope) {
      this.flag = scope.row.productType === '彩印'
      this.flag2 = scope.row.colorInMaterial === ''
      this.purAddVisible = true
      this.titleType = '编辑'
      getById(scope.row.pid).then(res => {
        this.formAdd = res
        this.formAdd.orderQuantity = res.orderQuantity
        // 加载供应商下拉框
        supplierSelect().then(res => {
          this.supplierFor = res
        })
        this.$set(this.formAdd, 'customerName', scope.row.name)
        this.$set(this.formAdd, 'taskNumber', scope.row.no)
        this.$set(this.formAdd, 'customerNo', scope.row.customerNo)
        this.$set(this.formAdd, 'supplierId', scope.row.supplier)
        this.$set(this.formAdd, 'customerNo', scope.row.customerNo)
        this.$set(this.formAdd, 'colorOutMaterial', scope.row.colorOutMaterial)
        this.$set(this.formAdd, 'colorInMaterial', scope.row.colorInMaterial)
        if (scope.row.isProduct === '成品') {
          this.$set(this.formAdd, 'isProduct', true)
        }
      })
    },

    // 新增订单
    purAdd(scope) {
      this.flag = scope.row.productType === '彩印'
      this.flag2 = scope.row.colorInMaterial === ''
      if (scope.row.isProduct === '成品') {
        this.isProducts = true
      }
      this.purAddVisible = true
      this.titleType = '新增'
      // 新增初始化数据
      this.formAdd = {}
      // 加载供应商下拉框
      supplierSelect().then(res => {
        this.supplierFor = res
      })
      this.formAdd = scope.row
      // this.supplierFor = scope.row.supplier
      this.$set(this.formAdd, 'customerName', scope.row.name)
      this.$set(this.formAdd, 'taskNumber', scope.row.no)
      this.$set(this.formAdd, 'customerNo', scope.row.customerNo)
      this.$set(this.formAdd, 'orderQuantity', scope.row.orderNum)
      this.$set(this.formAdd, 'supplierId', scope.row.supplier)
      this.$set(this.formAdd, 'insuppier', scope.row.insuppier)
      this.$set(this.formAdd, 'outsuppier', scope.row.outsuppier)
      this.formAdd.position = 0
      this.formAdd.endProductPos = 0
      if (scope.row.isProduct === '成品') {
        this.$set(this.formAdd, 'isProduct', true)
      }
    },
    // 取消
    purAddNo(purForm) {
      this.purAddVisible = false
      this.addTableData = []
      this.$refs[purForm].resetFields()
    },
    // 保存
    purAddOk(purForm) {
      this.$refs[purForm].validate((valid) => {
        if (valid) {
          this.formAdd.amount = parseFloat(this.formAdd.amount).toFixed(2)
          this.formAdd.costPrice = parseFloat(this.formAdd.costPrice).toFixed(2)
          add(this.formAdd).then(res => {
            if (res) {
              this.$message.success(this.titleType + '成功')
              this.$refs[purForm].resetFields()
              this.loadData()
            } else {
              this.$message.error(this.titleType + '失败')
            }
          })
          this.purAddVisible = false
        } else {
          return false
        }
      })
    }
  }
}

</script>
<style scoped lang="scss">
  >>>.el-dialog__header {
    border: 1px;
  }
  table{
  width:400px;
  border-collapse:collapse;
  height: auto;
  margin: 0 auto;
  margin-right: 250px;
}
tr{
  height: 35px;
}
table td{
  height: 50px;
}
td{
  width: 150px;
  text-align: center ;
  line-height: 35px;
}
table,tr,td{
  border:1px solid black;
}
.ups{
  line-height: 10px;
  color: #080e11;
}
.up{
  font-weight: bold;
}
</style>
